package LinkedList;

import java.util.Stack;

import static LinkedList.SingleLinkedList.reversetLinkedList;
import static LinkedList.SingleLinkedList.reversetPrint;

public class SingleLinkedListDemo {

  public static void main(String[] args) {
//    创建节点
    HeroNode node1 = new HeroNode(1, "will ospreay", "sky king");
    HeroNode node2 = new HeroNode(2, "kenny omega", "best bout machine");
    HeroNode node3 = new HeroNode(3, "kazuchika okada", "rainmaker");

//    创建单链表
    SingleLinkedList singleLinkedList = new SingleLinkedList();

//    将节点添加到链表中
//    singleLinkedList.add(node1);
//    singleLinkedList.add(node2);
//    singleLinkedList.add(node3);

//    按编号添加
    singleLinkedList.addByOrder(node1);
    singleLinkedList.addByOrder(node3);
    singleLinkedList.addByOrder(node2);

    System.out.println("修改前的链表情况");
    singleLinkedList.list();

    HeroNode node4 = new HeroNode(2, "jay white", "switch blade");
    singleLinkedList.update(node4);

    System.out.println("修改后的链表情况");
    singleLinkedList.list();

    System.out.println("删除后的链表情况");
    singleLinkedList.delete(1);
    singleLinkedList.list();

    System.out.println("节点的有效个数为：" + SingleLinkedList.getLength(singleLinkedList.getHead()));

//    倒数第k个节点
    System.out.println(SingleLinkedList.findLastNode(singleLinkedList.getHead(), 2));

//    输出反转链表
//    System.out.println("反转链表");
//    reversetLinkedList(singleLinkedList.getHead());
//    singleLinkedList.list();

//    从尾部到头部打印元素
    System.out.println("从尾部到头部打印元素:");
    reversetPrint(singleLinkedList.getHead());

  }
}

class SingleLinkedList{

  //先初始化一个头节点,头节点不要动,不存放具体的数据
  private HeroNode head = new HeroNode(0,"","");

  public HeroNode getHead() {
    return head;
  }

  public void add(HeroNode node){
//    添加的话要找到next为null的节点，将next修改为新添加的值
   HeroNode temp = head;
   while (true){
     if (temp.next == null){
       break;
     }
     temp = temp.next;
   }
   temp.next = node;
  }

  public void addByOrder(HeroNode node){
    boolean flag = false;
    HeroNode temp = head;

    while (true){
//      找到最后一个位置
      if (temp.next == null){
        break;
      }
      if (temp.next.no > node.no){
        break;
      }
      if (temp.next.no == node.no){
        flag = true;
        break;
      }
//      若上边3个条件都不满足，指针向后移动一位
      temp = temp.next;
    }
    if (flag){
      System.out.println("该节点"+ temp.next.no +"已经存在");
    }else {
      node.next = temp.next;
      temp.next = node;
    }

  }

  public void update(HeroNode newNode){
    boolean flag = false;
    HeroNode temp = head;

    while (true){
//      此时是最后一个
      if (temp.next == null){
        break;
      }
//      找到了，就是这个节点
      if (temp.no == newNode.no){
        flag = true;
        break;
      }
      temp = temp.next;
    }
    if (flag){
      temp.nickName = newNode.nickName;
      temp.name = newNode.name;
    }
  }

//  找到删除节点的前一个
  public void delete(int no){
    HeroNode temp = head;
    boolean flag = false;

    while (true){

//      表示此时是最后一个节点
      if (temp.next == null){
        break;
      }
      if (temp.next.no == no){
        flag = true;
        break;
      }
      temp = temp.next;

    }
    if (flag){
      temp.next = temp.next.next;
    }else {
      System.out.println("找不到该节点");
    }
  }

  public void list(){
//    若只有一个头结点，则直接返回
    if (head.next == null){
      System.out.println("链表为空");
      return;
    }
    HeroNode temp = head.next;
    while (true){
//      判断链表是否到最后
      if (temp == null){
        break;
      }
      System.out.println(temp);
//      输出完后要指向下一个节点
      temp = temp.next;
    }
  }

//  从尾部到头部打印元素
  public static void reversetPrint(HeroNode node){
    if (node.next == null){
      System.out.println("链表为空");
      return;
    }
    Stack<HeroNode> stack = new Stack<>();
    HeroNode cur = node.next;
    while (cur != null){
      stack.push(cur);
      cur =  cur.next;
    }
    while (stack.size()>0){
      System.out.println(stack.pop());
    }
  }

//  将单链表反转
  public static void reversetLinkedList(HeroNode node){
    if (node.next == null || node.next.next == null){
      return;
    }
//    用来保存当前节点指向的下一个节点
    HeroNode next = null;
    HeroNode reverstNode = new HeroNode(0,"","");
    HeroNode cur = node.next;
    while (cur!=null){
      next = cur.next;
      cur.next = reverstNode.next;
      reverstNode.next = cur;
      cur = next;
    }
    node.next = reverstNode.next;
  }

  public static HeroNode findLastNode(HeroNode head,int index){

//    假如只有头结点，返回null
    if (head.next == null){
      return null;
    }
//    先获得有效节点个数，再减去index，就是要找的节点的位置
    int size = getLength(head);
    if (index <=0 || index > size){
      return  null;
    }
    HeroNode cur = head.next;
    for (int i = 0; i < size - index;i++){
      cur = cur.next;
    }
    return cur;
  }

  public static int getLength(HeroNode head){
    if (head.next == null){
      return 0;
    }
    int length = 0;
    HeroNode cur = head.next;
    while (cur != null){
      length++;
      cur = cur.next;
    }
    return length;
  }
}

class HeroNode{

  public String name;
  public String nickName;
  public int no;
  public HeroNode next;

  public HeroNode(int no,String name,String nickName){
    this.no = no;
    this.name = name;
    this.nickName = nickName;
  }

  @Override
  public String toString() {
    return "HeroNode{" +
            "name='" + name + '\'' +
            ", nickName='" + nickName + '\'' +
            ", no=" + no + "}";
  }
}
